package com.bcst.utils;

import java.sql.SQLException;

/*
 *@description 欢迎关注公众号 编程识堂,每日更新技术相关文章，关注我不迷路
 *@author 小堂
 *@create 2023/9/2 16:11
 * 事务管理器类：负责手动事务的开启、提交、回滚
 */
public class TransactionManager {

   /* private TransactionManager() {

    }
    private static TransactionManager transactionManager = new TransactionManager();

    public static TransactionManager getInstance() {
        return transactionManager;
    }*/

   private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        //ConnectionUtils.getInstance().getCurrentThreadConn().setAutoCommit(false);
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }


    // 提交事务
    public void commit() throws SQLException {
        System.out.println("事务提交了。。。。。");
        //ConnectionUtils.getInstance().getCurrentThreadConn().commit();
        connectionUtils.getCurrentThreadConn().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        System.out.println("事务回滚了。。。。。");
        //ConnectionUtils.getInstance().getCurrentThreadConn().rollback();
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
